package blackitheima1;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

//stream流
public class MyStream1 {
    public static void main(String[] args) {
        //toList与toSet方法收集数据
        //创建集合
        ArrayList<Integer> list=new ArrayList<>();
        //遍历添加元素
        for (int i = 1; i <= 10; i++) {
            list.add(i);
        }
        list.add(10);
        list.add(10);
        list.add(10);
        list.add(10);
        list.add(10);
        //创建stream流
        //filter:过滤
        //collect:收集数据
        //collectors:collector的工具类可调用方法
        List<Integer> list1 = list.stream().filter(s -> s % 2 == 0).collect(Collectors.toList());
        System.out.println(list1);

        System.out.println("====================");
        //创建stream流  set集合不能有重复数存在
        Set<Integer> set = list.stream().filter(s -> s % 2 == 0).collect(Collectors.toSet());
        System.out.println(set);

        //创建集合
        ArrayList<String> list2=new ArrayList<>();
        list2.add("张三,23");
        list2.add("李四,21");
        list2.add("胡歌,25");
        list2.add("狄仁杰,22");
        //创建stream流
        Map<String, Integer> map = list2.stream().filter(
                s -> {
                    String[] split = s.split(",");
                    //字符串转int类型,才能比较数字大小
                    int i = Integer.parseInt(split[1]);
                    return i >= 22;
                }
        ).collect(Collectors.toMap(
                //字符串分割
               /* (String s) -> {
                    String[] split = s.split(",");
                    return split[0];
                },
                (String s) -> {
                    String[] split = s.split(",");
                    return Integer.parseInt(split[1]);
                }*/
               s->s.split(",")[0],
                s->Integer.parseInt(s.split(",")[1])
        ));
        System.out.println(map);


    }
}
